import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_business_core/business_retails/components/business_retails_order_style_item.dart';

/// 界面名称: 零售单详情
/// 参数: 需要外部传入一个对象 对象里面要包含单据编号 billsId
class BusinessRetailsOrderDetails extends StatelessWidget {

  static final String sName = 'user/myretail/details';

  @override
  Widget build(BuildContext context) {
    dynamic arguments = BaseRouterUtil.getArguments(context);
    String billsId = BaseUtils.getKeyData(arguments, 'billsId').toString();
    return BaseScaffoldComponent(
      title: '零售单详情',
      child: BaseScreenRefreshComponent(
        request: new BaseScreenRequestModel('/retail/retails/list/retail/retailmainandsubs', params: {'billsId': billsId}),
        controller: new BaseFutureController(),
        builder: (BaseHttpResultModel result) => _buildRetails(context, result),
      ),
    );
  }

  Widget _buildRetails(BuildContext context, BaseHttpResultModel result) {
    dynamic data = result.data;
    return ListView(
      padding: const EdgeInsets.only( bottom: 12.0 ),
      children: [
        _buildRetailsHeader(data),
        _buildRetailsVipContent(context, data),
        _buildRetailsStyle(context, data),
        _buildRetailsPayTypes(context, data),
        _buildRetailsClerksRatio(context, data),
        _buildRetailsPickMethod(context, data),
      ],
    );
  }

  /// 零售单详情-头部
  Widget _buildRetailsHeader(dynamic data) {
    String billsId = BaseUtils.getKeyData(data, 'billsId').toString();
    String saleDate = BaseUtils.getKeyData(data, 'saleDate').toString();
    String dataSource = BaseUtils.getKeyData(data, 'dataSource').toString();
    String setdepotName = BaseUtils.getKeyData(data, 'setdepotName').toString();
    String setdepotId = BaseUtils.getKeyData(data, 'setdepotId').toString();
    String systemTypeName = BaseUtils.getKeyData(data, 'systemTypeName').toString();
    return BaseCardComponent(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          BaseComponentUtils.buildTextSpanWidget(['单据编号:', billsId]),
          SizedBox(height: 8.0),
          BaseComponentUtils.buildTextSpanWidget(['销售店铺:', '[$setdepotId]$setdepotName']),
          SizedBox(height: 8.0),
          BaseComponentUtils.buildTextSpanWidget(['销售时间:', BaseTransformUtil.stringToDateTimeStamp(saleDate, isNeedShowSecond: false)]),
          SizedBox(height: 8.0),
          BaseComponentUtils.buildTextSpanWidget(['零售类型:', systemTypeName]),
          SizedBox(height: 8.0),
          BaseComponentUtils.buildTextSpanWidget(['数据来源:', dataSource]),
        ],
      ),
    );
  }

  /// 是否存在会员 有会员则显示会员详情
  Widget _buildRetailsVipContent(BuildContext context, dynamic data) {
    String vipId = BaseUtils.getKeyData(data, 'vipId').toString();
    return vipId.length > 0 ? _buildRetailsVipListItem(context, data) : SizedBox(height: 0);
  }

  /// 构建会员详情布局
  Widget _buildRetailsVipListItem(BuildContext context, dynamic data) {
    return BaseTitleCardComponent( title: '会员信息', child: this._buildRetailsVipListItemContent(context, data) );
  }

  Widget _buildRetailsVipListItemContent(BuildContext context, dynamic data) {
    String vipId = BaseUtils.getKeyData(data, 'vipId').toString();
    String vipName = BaseUtils.getKeyData(data, 'vipName').toString();
    String gradeName = BaseUtils.getKeyData(data, 'gradeName').toString();
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        BaseImageComponent(imageUrl: 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1593106255,4245861836&fm=26&gp=0.jpg', width: 32.0, height: 32.0, type: BaseImageTypeEnum.X3User, placeholderName: vipName, circular: 30.0,),
        SizedBox(width: 12.0),
        Text('$vipName($vipId)'),
        SizedBox(width: 2.0),
        Text(gradeName, style: Theme.of(context).textTheme.subtitle1.copyWith(color: Color(0xFFBF9C4F))),
      ],
    );
  }

  /// 构建款式明细列表
  Widget _buildRetailsStyle(BuildContext context, dynamic data) {
    return BaseTitleCardComponent( title: '商品明细', child: this._buildRetailsStyleContent(context, data) );
  }

  Widget _buildRetailsStyleContent(BuildContext context, dynamic data) {
    List<dynamic> styles = BaseUtils.getKeyData(data, 'billSubList');
    String billsSSums = BaseUtils.getKeyData(data, 'billsSSums').toString();
    String accFsums = BaseUtils.getKeyData(data, 'accFsums').toString();
    String accTotalLpriceSums = BaseUtils.getKeyData(data, 'accTotalLpriceSums').toString();
    String avgDiscount = BaseUtils.getKeyData(data, 'avgDiscount').toString();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        ...styles.map((e) => BusinessRetailsOrderStyleItem(styleItem: e)).toList(),
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [Text('零售总价'), Text(accTotalLpriceSums)]),
        SizedBox(height: 8.0),
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [Text('平均折扣'), Text(avgDiscount)]),
        SizedBox(height: 8.0),
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [Text('结算金额'), Text(billsSSums)]),
        SizedBox(height: 8.0),
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [Text('实销金额'), Text(accFsums)]),
      ],
    );
  }

  Widget _buildRetailsPayTypes(BuildContext context, dynamic data) {
    List<dynamic> payTypes = BaseUtils.getKeyData(data, 'payList');
    return BaseTitleCardComponent( title: '收款方式', child: Column(children: payTypes.map((e) => _buildPayTypeItem(e)).toList()) );
  }

  Widget _buildPayTypeItem(dynamic item) {
    String payName = BaseUtils.getKeyData(item, 'payName').toString();
    String paySums = BaseUtils.getKeyData(item, 'paySums').toString();
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(bottom: 12.0),
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [Text(payName), Text(paySums)]),
    );
  }

  Widget _buildRetailsClerksRatio(BuildContext context, dynamic data) {
    List<dynamic> retailSales = BaseUtils.getKeyData(data, 'retailSale');
    double accFsums = BaseDefaultsValue.doubleDefaults(data['accFsums']);
    return BaseTitleCardComponent( title: '业绩分配', child: Column(children: retailSales.map((e) => _buildRetailsClerksRatioItem(e, accFsums)).toList()) );
  }

  Widget _buildRetailsClerksRatioItem(dynamic item, double accFsums) {
    String saleName = BaseUtils.getKeyData(item, 'saleName').toString();
    String saleTag = BaseUtils.getKeyData(item, 'saleTag').toString();
    double accDiscount = BaseDefaultsValue.doubleDefaults(item['accDiscount']);
    String saleImgUrl = BaseUtils.getKeyData(item, 'saleImgUrl').toString();
    String result = (accDiscount * accFsums).toString();
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(bottom: 12.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          BaseImageComponent(imageUrl: saleImgUrl, placeholderName: saleName, width: 30.0, height: 30.0, circular: 30.0, size: BaseImageSizeEnum.original, type: BaseImageTypeEnum.X3User,),
          SizedBox(width: 16.0),
          Expanded(child: Text(saleName)),
          Text("${(accDiscount * 100).toStringAsFixed(2)}%"),
          BaseComponentUtils.buildTextSpanWidget([saleTag, '($result)']),
        ],
      ),
    );
  }

  Widget _buildRetailsPickMethod(BuildContext context, dynamic data) {
    String cnee = BaseUtils.getKeyData(data, 'cnee').toString();
    String cneeTel = BaseUtils.getKeyData(data, 'cneeTel').toString();
    return (cnee.length > 0 && cneeTel.length > 0) ? BaseTitleCardComponent( title: '提货方式', child: this._buildRetailsPickMethodContent(context, data) ) : SizedBox();
  }

  Widget _buildRetailsPickMethodContent(BuildContext context, dynamic data) {
    String cnee = BaseUtils.getKeyData(data, 'cnee').toString();
    String cneeTel = BaseUtils.getKeyData(data, 'cneeTel').toString();
    String cneeAreaName = BaseUtils.getKeyData(data, 'cneeAreaName').toString();
    String cneeCityName = BaseUtils.getKeyData(data, 'cneeCityName').toString();
    String cneeProvinceName = BaseUtils.getKeyData(data, 'cneeProvinceName').toString();
    String cneeAddr = BaseUtils.getKeyData(data, 'cneeAddr').toString();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        BaseComponentUtils.buildTextSpanWidget([cnee, cneeTel]),
        SizedBox(height: 8.0),
        Text(
          '$cneeProvinceName$cneeCityName$cneeAreaName$cneeAddr',
          style: Theme.of(context).textTheme.subtitle2,
        ),
      ],
    );
  }

}
